package main

import (
	"fmt"
	"time"
)

func main() {
	//时间对象
	now := time.Now()
	fmt.Println(now)
	//格式化时间
	//格式内容必须是2006-01-02 15:04:05
	nowStr := now.Format("2006-01-02 15:04:05")
	fmt.Println(nowStr)
	//时间戳
	//秒
	fmt.Println(now.Unix())
	//毫秒
	fmt.Println(now.UnixMilli())
	//时间戳转成时间对象
	timmeObj := time.Unix(now.Unix(), 0)
	fmt.Println(timmeObj)
	//格式化时间（字符串）转成时间对象
	loc, _ := time.LoadLocation("Asia/Shanghai")
	timmeObj, _ = time.ParseInLocation("2006-01-02 15:04:05", nowStr, loc)
	fmt.Println(timmeObj)

	//通过时间对象单独获取年月日时分秒
	year := now.Year()
	month := now.Month()
	day := now.Day()
	hour := now.Hour()
	minute := now.Minute()
	second := now.Second()
	fmt.Println(year, month, day, hour, minute, second)
	fmt.Printf("%d-%d-%d %d:%d:%d\n", year, month, day, hour, minute, second)
	fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)

	//时间运算
	m, _ := time.ParseDuration("-1m")
	m1 := now.Add(m)
	fmt.Println(m, m1)
	//差值
	now1 := time.Now()
	m2 := now1.Sub(now)
	fmt.Println(m2)
}
